package com.instalinks.android.smspostservice.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.instalinks.android.smspostservice.R;
import com.instalinks.android.smspostservice.service.AppService;
import com.instalinks.android.smspostservice.utils.enums.AppActivities;

public class AppNotifications {

	private static final String NOTIFICATION_TITLE = "Instalinks Thin Server";
	private static final String NOTIFICATION_TITLE_EXTERNAL = "Thin Server";
	private static final String SERVICE_DISABLED = "Service Disabled";
	private static final String SERVICE_ENABLED = "Service Enabled";
	private static final String SERVICE_RUNNING = "Service Running...";

	private Context mContext;
	private NotificationManager mNotificationManager;
	private AppConfig mAppConfig;

	public AppNotifications(Context context) {
		this.mContext = context;
		this.mAppConfig = new AppConfig(context);

		mNotificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	public void notifyServiceState() {
		if (mAppConfig.isServiceRunning()) {
			mNotificationManager.notify(
					Constants.SERVICE_NOTIFICATION_ID,
					getMessageNotification(SERVICE_ENABLED,
							R.drawable.service_enable, false));
		} else {
			mNotificationManager.notify(
					Constants.SERVICE_NOTIFICATION_ID,
					getMessageNotification(SERVICE_DISABLED,
							R.drawable.service_disable, false));
		}
	}

	public void notifyServiceEnabled() {
		mNotificationManager.notify(
				Constants.SERVICE_NOTIFICATION_ID,
				getMessageNotification(SERVICE_ENABLED,
						R.drawable.service_enable, false));
	}

	public void notifyServiceRunning() {
		mNotificationManager.notify(
				Constants.SERVICE_NOTIFICATION_ID,
				getMessageNotification(SERVICE_RUNNING,
						R.drawable.service_enable, false));
	}

	public void notifyServiceDisabled() {
		mNotificationManager.notify(
				Constants.SERVICE_NOTIFICATION_ID,
				getMessageNotification(SERVICE_DISABLED,
						R.drawable.service_disable, false));
	}

	public void notifyClearAll() {
		mNotificationManager.cancel(Constants.SERVICE_NOTIFICATION_ID);
	}

	public void notifyGenericMessage(String msg) {
		mNotificationManager.notify(Constants.SERVICE_NOTIFICATION_ID,
				getMessageNotification(msg, getServiceStatusIcon(), true));
	}

	public void notifyGenericMessage(String msg, AppActivities activity) {
		mNotificationManager.notify(
				Constants.SERVICE_NOTIFICATION_ID,
				getMessageNotification(msg, getServiceStatusIcon(), true,
						activity));
	}

	public void notifyGenericMessageExternal(String msg, boolean allowSound) {
		notifyGenericMessageExternal(msg, allowSound, AppActivities.Home);
	}
	
	public void notifyGenericMessageExternal(String msg, boolean allowSound,
			AppActivities activity) {
		mNotificationManager.notify(
				Constants.EXTERNAL_NOTIFICATION_ID,
				getExternalNotification(msg, allowSound,
						activity));
	}
	

	// **********************//

	private Notification getMessageNotification(String msg, int icon,
			boolean allowSound) {
		return getNotification(msg, icon, NOTIFICATION_TITLE, msg, allowSound);
	}

	private Notification getMessageNotification(String msg, int icon,
			boolean allowSound, AppActivities activity) {
		return getNotification(msg, icon, NOTIFICATION_TITLE, msg, allowSound,
				activity);
	}

	// **********************//
	private Notification getNotification(String notification_title, int icon,
			String content_title, String content_text, boolean allowSound) {

		return getNotification(notification_title, icon, content_title,
				content_text, allowSound, AppActivities.Home);
	}

	private Notification getNotification(String notification_title, int icon,
			String content_title, String content_text, boolean allowSound,
			AppActivities activity) {
		long when = System.currentTimeMillis();

		CharSequence notificationTitle = notification_title;
		CharSequence contentTitle = content_title;
		CharSequence contentText = content_text;

		Notification notification = new Notification();
		notification.tickerText = notificationTitle;
		notification.when = when;
		notification.icon = icon;

		notification.flags = Notification.FLAG_ONGOING_EVENT;

		if (allowSound)
			notification.defaults |= Notification.DEFAULT_SOUND;

		Intent notificationIntent = getActivityIntent(activity);

		PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
				notificationIntent, 0);

		notification.setLatestEventInfo(mContext, contentTitle, contentText,
				contentIntent);

		return notification;
	}
	
	private Notification getExternalNotification(String message, boolean allowSound,
			AppActivities activity) {
		long when = System.currentTimeMillis();

		CharSequence notificationTitle = message;
		CharSequence contentTitle = NOTIFICATION_TITLE_EXTERNAL;
		CharSequence contentText = message;

		Notification notification = new Notification();
		notification.tickerText = notificationTitle;
		notification.when = when;
		notification.icon = R.drawable.external_notification;

		notification.flags = Notification.FLAG_AUTO_CANCEL;

		if (allowSound)
			notification.defaults |= Notification.DEFAULT_SOUND;

		Intent notificationIntent = getActivityIntent(activity);

		PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
				notificationIntent, 0);

		notification.setLatestEventInfo(mContext, contentTitle, contentText,
				contentIntent);
		
		return notification;

		
	}

	private Intent getActivityIntent(AppActivities activity) {
		Intent notificationIntent = new Intent(Intent.ACTION_MAIN);

		if (activity == AppActivities.Home) {

			notificationIntent
					.setClass(
							mContext,
							com.instalinks.android.smspostservice.activity.HomeActivity.class);
		} else if (activity == AppActivities.Notification) {
			notificationIntent
					.setClass(
							mContext,
							com.instalinks.android.smspostservice.activity.NotificationsActivity.class);
		} else if (activity == AppActivities.MessageInbox) {
			notificationIntent.setType("vnd.android-dir/mms-sms");
		}

		return notificationIntent;
	}

	protected final int getServiceStatusIcon() {
		if (isServiceRunning()) {
			return R.drawable.service_enable;
		} else {
			return R.drawable.service_disable;
		}
	}

	public final boolean isServiceRunning() {
		ActivityManager manager = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		for (RunningServiceInfo srv : manager
				.getRunningServices(Integer.MAX_VALUE)) {
			if (srv.service.getClassName().equals(AppService.class.getName())) {
				return true;
			}
		}
		return false;
	}
}
